package com.hepinghospital.user.ui.fragment;

import android.content.Context;
import android.os.Bundle;

import com.google.android.material.tabs.TabLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import butterknife.BindView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseFragment;

import org.jetbrains.annotations.NotNull;

import java.util.Objects;

/**
 *
 * @author HP
 * @date 2020/2/25
 */

public class MyCloudOrderFragment extends BaseFragment {

    @BindView(R.id.timeline_tablayout)
    TabLayout mTabLayout;
    @BindView(R.id.timeline_viewpager)
    ViewPager mViewPager;

    private int page;

    @Override
    protected void initView() {
        mViewPager.setAdapter( new CloudFragmentAdapter( getActivity(), getChildFragmentManager() ) );
        mTabLayout.setupWithViewPager( mViewPager );
        mTabLayout.setTabMode( TabLayout.MODE_SCROLLABLE );

        mViewPager.setCurrentItem( page );
        Objects.requireNonNull( mTabLayout.getTabAt( page ) ).select();
    }

    public static MyCloudOrderFragment newInstance(int type, int page) {
        Bundle args = new Bundle();
        args.putInt( "type", type );
        args.putInt( "page", page );
        MyCloudOrderFragment fragment = new MyCloudOrderFragment();
        fragment.setArguments( args );
        return fragment;
    }


    @Override
    protected void initData() {
        assert getArguments() != null;
        int type = getArguments().getInt( "type" );
        page = getArguments().getInt( "page" );
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_my_cloud;
    }

    static class CloudFragmentAdapter extends FragmentPagerAdapter {
        private static final int PAGE_COUNT = 7;

        CloudFragmentAdapter(Context context, FragmentManager fm) {
            super( fm );
        }

        @NotNull
        @Override
        public Fragment getItem(int position) {
            String type;
            switch (position) {
                case 0:
                    type = "0";
                    break;
                case 1:
                    type = "1";
                    break;
                case 2:
                    type = "2";
                    break;
                case 3:
                    type = "3";
                    break;
                case 4:
                    type = "4";
                    break;
                case 5:
                    type = "7";
                    break;
                default:
                    type = "5";
                    break;
            }
            return CloudListFragment.newInstance( type );
        }

        @Override
        public int getCount() {
            return PAGE_COUNT;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "全部";
                case 1:
                    return "待支付";
                case 2:
                    return "未开始";
                case 3:
                    return "进行中";
                case 4:
                    return "待评价";
                case 5:
                    return "已完成";
                default:
                    return "已取消";
            }
        }

    }

}
